import asyncio
import os

from glados import GLaDOSCheckIn, GLaDOSCheckInWebHook, GLaDOSCheckInTgBot, GLaDOSCheckInWeChat

COOKIES = os.getenv("COOKIES")

BOT_TOKEN = os.getenv("BOT_TOKEN")
CHAT_ID = os.getenv("CHAT_ID")

WEBHOOK_URL = os.getenv("WEBHOOK_URL")
WEBHOOK_TOKEN = os.getenv("WEBHOOK_TOKEN")

WECHAT_CORP_ID = os.getenv("WECHAT_CORP_ID")
WECHAT_AGENT_ID = os.getenv("WECHAT_AGENT_ID")
WECHAT_CORP_SECRET = os.getenv("WECHAT_CORP_SECRET")
WECHAT_TO_USER = os.getenv("WECHAT_TO_USER")


# noinspection PyUnusedLocal
def handler(event, context):
    main()
    return "Done"


async def tg_bot_check_in() -> None:
    if COOKIES is None:
        raise ValueError("Environment COOKIES not found!")
    if BOT_TOKEN is None:
        raise ValueError("Environment BOT_TOKEN not found!")
    if CHAT_ID is None:
        raise ValueError("Environment CHAT_ID not found!")
    async with GLaDOSCheckInTgBot(COOKIES, BOT_TOKEN, CHAT_ID) as glados:
        await glados.run_check_in()


async def webhook_check_in() -> None:
    if COOKIES is None:
        raise ValueError("Environment COOKIES not found!")
    if WEBHOOK_URL is None:
        raise ValueError("Environment WEBHOOK_URL not found!")
    if WEBHOOK_TOKEN is None:
        raise ValueError("Environment WEBHOOK_TOKEN not found!")
    async with GLaDOSCheckInWebHook(COOKIES, WEBHOOK_URL, WEBHOOK_TOKEN) as glados:
        await glados.run_check_in()


async def wechat_check_in() -> None:
    if COOKIES is None:
        raise ValueError("Environment COOKIES not found!")
    if WECHAT_CORP_ID is None:
        raise ValueError("Environment WECHAT_CORP_ID not found!")
    if WECHAT_AGENT_ID is None:
        raise ValueError("Environment WECHAT_AGENT_ID not found!")
    if WECHAT_CORP_SECRET is None:
        raise ValueError("Environment WECHAT_CORP_SECRET not found!")
    if WECHAT_TO_USER is None:
        raise ValueError("Environment WECHAT_TO_USER not found!")
    async with GLaDOSCheckInWeChat(COOKIES, WECHAT_CORP_ID, WECHAT_AGENT_ID, WECHAT_CORP_SECRET, WECHAT_TO_USER) as glados:
        await glados.run_check_in()


async def local_check_in() -> None:
    if COOKIES is None:
        raise ValueError("Environment COOKIES not found!")
    async with GLaDOSCheckIn(COOKIES) as glados:
        await glados.run_check_in()


def main() -> None:
    looper = asyncio.get_event_loop()
    try:
        looper.run_until_complete(tg_bot_check_in())
        # looper.run_until_complete(webhook_check_in())
        # looper.run_until_complete(wechat_check_in())
        # looper.run_until_complete(local_check_in())
    finally:
        if not looper.is_closed:
            looper.close()


if __name__ == "__main__":
    main()
